<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\users;


Class UsersModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        $users = $this->em->getRepository('BillingBundle:users')->findAll();
        return $users;
    }
    
    public function findByPK($user_id)
    {
        $user = $this->em->find('BillingBundle:users', $user_id);
        return $user;
    }

    public function findByLogin($login)
    {
        $user = $this->em->getRepository('BillingBundle:users')->findOneBy(array('login' => $login));
        if (isset($user))
            return $user;
        else
            return false;
    }

    public function findAllAdmins()
    {
        $users = $this->em->getRepository('BillingBundle:users')->findBy(array('status_id' => '1'));
        return $users;
    }

    public function findByHash($sHash, $salt)
    {
        $users = $this->em->getRepository('BillingBundle:users')->findAll();
        foreach ($users as $user)
        {
            if ( md5( sha1( $salt.$user->getLogin().$user->getPassword() ).$user->getId() ) == $sHash )
                return $user;
        }
        return new users();
    }

    public function checkByLogin($login)
    {
        $user = $this->em->getRepository('BillingBundle:users')->findOneBy(array('login' => $login));
        if (isset($user))
            return true;
        else
            return false;
    }
    
    public function create(users $oEntity)
    {
        $this->em->persist($oEntity);
        $this->em->flush();
    }

    public function removeByPK($user_id)
    {
        $user = $this->em->find('BillingBundle:users', $user_id);
        $this->em->remove($user);
        $this->em->flush();
    } 
}
?>
